#include "threadpool.h"

#include <QThread>
#include <QDebug>

ThreadPool::ThreadPool()
{
	// init Thread use CPU counts
	int cpuCnt = QThread::idealThreadCount();
	int initThreadCnt = (cpuCnt > 0) ? cpuCnt : 4;
	m_threadPool.setMaxThreadCount(initThreadCnt);
	m_threadPool.setExpiryTimeout(3000);		// 闲置回收
	qDebug() << "[ThreadPool] 初始化完成，最大线程数：" << initThreadCnt;
}

ThreadPool &ThreadPool::instance()
{
	static ThreadPool s_instance; // 静态单例（线程安全）
    return s_instance;
}

void ThreadPool::setMaxThreadCount(int maxThreadCount)
{
	if (maxThreadCount > 0 && maxThreadCount != m_threadPool.maxThreadCount()) {
		m_threadPool.setMaxThreadCount(maxThreadCount);
		qDebug() << "[ThreadPool] 最大线程数更新为：" << maxThreadCount;
	}
}

int ThreadPool::maxThreadCount() const
{
	return m_threadPool.maxThreadCount();
}

bool ThreadPool::waitForDone(int msecs)
{
	return m_threadPool.waitForDone(msecs);
}

